package com.user.controller;

import com.common.entity.sys.Dict;
import com.github.pagehelper.PageInfo;
import com.user.service.IDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: 温洁宇
 * @date: 2021/08/11/19:00
 * Other: 系统字典控制器
 */
@RestController
@RequestMapping("/dict")
@CrossOrigin
public class SysDictController {

    @Autowired
    private IDictService dictService;

    /**
     * 查询字典
     */
    @PostMapping("/findAll")
    public PageInfo<Dict> findAll(
            @RequestBody Dict dict,
            @RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize",defaultValue = "10") int pageSize
    ) {
        return dictService.findAll(dict,pageNum,pageSize);
    }

    /**
     * 添加子目录
     */
    @PostMapping("/addChildren")
    public boolean addChildren(@RequestBody Dict dict) {
        try {
            return dictService.addChildren(dict);
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 查询单个字典子集
     */
    @GetMapping("/findListByParentId")
    public List<Dict> findListByParentId(int parentId) {
        return dictService.findOne(parentId);
    }

    /**
     * 根据id查询
     */
    @GetMapping("/findOneById")
    public Dict findOneById(int id) {
        return dictService.findOneById(id);
    }

    /**
     * 删除字典
     */
    @DeleteMapping("/delOne")
    public boolean delOne(int id) {
        return dictService.delOne(id);
    }

}
